package com.example.myprojectoffeixue;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class HwKTActivity extends Activity implements OnClickListener {
    Map map;
    private controllerList c;
    private Vibrator vibrator;
    private HongWaiSB h;
    private MyApplication m;
    private boolean flag1;
    private EditText et;
    private LinearLayout l;
    private ImageView iv1;
    private ImageView iv2;
    private ImageView iv3;
    public ImageView tv;

    public TextView tv1;

    public LinearLayout tv2;

    public LinearLayout tv3;

    public TextView tv4;

    public TextView tv5;

    public TextView tv6;

    public TextView tv7;

    public ImageView tv10;

    public ImageView tv11;

    public LinearLayout tv14;
    public TextView tv15;
    public LinearLayout tv16;
    public TextView tv17;
    private String s;
    private AlertDialog.Builder builder;
    private LinearLayout l1;
    public String code = "25";
    public String mode = "02";
    public String flag = "00";
    public String speed = "01";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hw_kt);
        info();
    }

    private void info() {
        iv1 = (ImageView) findViewById(R.id.start_kongtiao);
        iv2 = (ImageView) findViewById(R.id.dianjizhire);
        iv3 = (ImageView) findViewById(R.id.iv_lengfeng);

        Intent it = getIntent();
        c = (controllerList) it.getSerializableExtra("controllerList");

        if (c.selfLearning == 1) {
            flag1 = true;

        }


        LinearLayout iv1 = (LinearLayout) findViewById(R.id.iv_kongtiao);

        iv1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();

            }
        });

        m = (MyApplication) getApplication();
        h = new HongWaiSB();
        h.cmd = "1012";
        h.extra = new Extra();

        h.status = "04";
        if (!flag1) {
            h.status = "01";

        }

            h.extra.type = c.kind;
            h.mac=m.mac_ykq;
            h.ieee = m.ieee_ykq;
            h.port = m.port_ykq;

        l1 = (LinearLayout) findViewById(R.id.lin_kongtiao);
        tv = (ImageView) findViewById(R.id.start_kongtiao);
        tv.setOnClickListener(this);

        tv2 = (LinearLayout) findViewById(R.id.lengfeng_kongtiao);
        tv2.setOnClickListener(this);

        tv3 = (LinearLayout) findViewById(R.id.nuanfeng_kongtiao);
        tv3.setOnClickListener(this);

        tv4 = (TextView) findViewById(R.id.zidongfeng_kongtiao);
        tv4.setOnClickListener(this);
        tv5 = (TextView) findViewById(R.id.digufeng_kongtiao);
        tv5.setOnClickListener(this);
        tv6 = (TextView) findViewById(R.id.zhongsu_kongtiao);
        tv6.setOnClickListener(this);
        tv7 = (TextView) findViewById(R.id.gaofeng_kongtiao);
        tv7.setOnClickListener(this);

        tv10 = (ImageView) findViewById(R.id.addwd_kongtiao);
        tv10.setOnClickListener(this);
        tv11 = (ImageView) findViewById(R.id.jswd_kongtiao);
        tv11.setOnClickListener(this);
        tv15 = (TextView) findViewById(R.id.wendu_kongtiao);

        vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);

    }

    @Override
    public void onClick(View v) {
        String l = "";

        switch (v.getId()) {

            case R.id.start_kongtiao:
                if ("01".equals(flag)) {
                    h.extra.powerStatus = "00";
                    flag = "00";
                    iv1.setImageResource(R.drawable.ktguan);
                } else {
                    h.extra.powerStatus = "01";
                    flag = "01";
                    iv1.setImageResource(R.drawable.start);
                }

                if (flag1) {

                    KTutil k = new KTutil();
                    k.controllerId = c.id + "";
                    k.switchFlg = flag;
                    k.speed = speed;
                    k.model = mode;
                    k.temperature = code;
                    String j = JSON.toJSONString(k);
                    j = AESUtil.encrypt(j);
                    HttpUtils httpUtils = new HttpUtils();
                    RequestParams params = new RequestParams();
                    params.setHeader("Content-Type", "application/json");
                    try {
                        params.setBodyEntity(new StringEntity(j, "utf-8"));
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    httpUtils.send(HttpRequest.HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/remote/loadArcCode",
                            params, new RequestCallBack<String>() {

                                @Override
                                public void onFailure(HttpException arg0, String arg1) {
                                    HttpConnection.showToast(HwKTActivity.this,"网络错误");

                                }

                                @Override
                                public void onSuccess(ResponseInfo<String> arg0) {
                                    if (arg0.result != null && !("".equals(arg0.result))) {
                                        arg0.result = AESUtil.decrypt(arg0.result);
                                        JSONObject j = JSONObject.parseObject(arg0.result);
                                        String originalCode = j.getString("originalCode");
                                        if (originalCode != null) {
                                            h.extra.code = originalCode;
                                            String l = JSON.toJSONString(h);
                                            if (m.flag3) {
                                                m.c.send(l);
                                                vibrator.vibrate(new long[]{0, 300}, -1);
                                            } else {

                                                HttpConnection.showToast(getBaseContext(),"命令发送失败，请重试");

                                            }

                                        } else {
                                            HttpConnection.showToast(getBaseContext(),"没有学习状态");
                                        }


                                    }else{
                                        HttpConnection.showToast(getBaseContext(),"服务器错误");
                                    }
                                }

                            });


                } else {
                    h.extra.codeIndex = c.controllerDetail.get(0).num + "";
                    h.extra.settingType = "01";
                    h.extra.temperature = code;
                    h.extra.mode = mode;
                    h.extra.speed = speed;
                    l = JSON.toJSONString(h);
                    if (m.flag3) {
                        m.c.send(l);
                        vibrator.vibrate(new long[]{0, 300}, -1);
                    } else {
                        HttpConnection.showToast(getBaseContext(),"命令发送失败，请重试");
                    }

                }
                break;

            case R.id.lengfeng_kongtiao:
                mode = "02";
                iv3.setImageResource(R.drawable.dianjizhileng);
                iv2.setImageResource(R.drawable.zhire);

                if (flag1) {
                    KTutil k = new KTutil();
                    k.controllerId = c.id + "";
                    k.switchFlg = flag;
                    k.speed = speed;
                    k.model = mode;
                    k.temperature = code;

                    String j = JSON.toJSONString(k);
                    j = AESUtil.encrypt(j);
                    HttpUtils httpUtils = new HttpUtils();
                    RequestParams params = new RequestParams();
                    params.setHeader("Content-Type", "application/json");
                    try {
                        params.setBodyEntity(new StringEntity(j, "utf-8"));
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    httpUtils.send(HttpRequest.HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/remote/loadArcCode",
                            params, new RequestCallBack<String>() {

                                @Override
                                public void onFailure(HttpException arg0, String arg1) {
                                    HttpConnection.showToast(getBaseContext(),"网络异常");

                                }

                                @Override
                                public void onSuccess(ResponseInfo<String> arg0) {
                                     if(arg0.result != null && !("".equals(arg0.result))){
                                    arg0.result = AESUtil.decrypt(arg0.result);
                                    JSONObject j = JSONObject.parseObject(arg0.result);
                                    String originalCode = j.getString("originalCode");
                                    if (originalCode != null) {

                                        h.extra.code = originalCode;
                                        String l = JSON.toJSONString(h);
                                        if (m.flag3) {
                                            m.c.send(l);

                                            vibrator.vibrate(new long[]{0, 300}, -1);
                                        } else {

                                            HttpConnection.showToast(getBaseContext(),"命令发送失败，请重试");

                                        }

                                    } else {
                                        HttpConnection.showToast(getBaseContext(),"没有学习状态");


                                    }}else{
                                         HttpConnection.showToast(getBaseContext(),"服务器错误");
                                     }


                                }

                            });


                } else {
                    h.extra.codeIndex = c.controllerDetail.get(0).num + "";

                    h.extra.settingType = "02";

                    h.extra.powerStatus = flag;
                    h.extra.temperature = code;
                    h.extra.mode = mode;
                    h.extra.speed = speed;
                    if (m.flag3) {
                        l = JSON.toJSONString(h);
                        m.c.send(l);
                    } else {

                        HttpConnection.showToast(getBaseContext(),"命令发送失败，请重试");

                    }
                    vibrator.vibrate(new long[]{0, 300}, -1);
                }


                break;

            case R.id.nuanfeng_kongtiao:
                mode = "05";
                iv2.setImageResource(R.drawable.dinajizhire);
                iv3.setImageResource(R.drawable.zhileng);
                if (flag1) {

                    KTutil k = new KTutil();
                    k.controllerId = c.id + "";
                    k.switchFlg = flag;
                    k.speed = speed;
                    k.model = mode;
                    k.temperature = code;

                    String j = JSON.toJSONString(k);
                    j = AESUtil.encrypt(j);
                    HttpUtils httpUtils = new HttpUtils();
                    RequestParams params = new RequestParams();
                    params.setHeader("Content-Type", "application/json");
                    try {
                        params.setBodyEntity(new StringEntity(j, "utf-8"));
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    httpUtils.send(HttpRequest.HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/remote/loadArcCode",
                            params, new RequestCallBack<String>() {

                                @Override
                                public void onFailure(HttpException arg0, String arg1) {
                                    HttpConnection.showToast(getBaseContext(),"网络异常");

                                }

                                @Override
                                public void onSuccess(ResponseInfo<String> arg0) {
                                    if (arg0.result != null && !("".equals(arg0.result))) {

                                        arg0.result = AESUtil.decrypt(arg0.result);
                                        JSONObject j = JSONObject.parseObject(arg0.result);
                                        String originalCode = j.getString("originalCode");
                                        if (originalCode != null) {

                                            h.extra.code = originalCode;
                                            String l = JSON.toJSONString(h);
                                            if (m.flag3) {
                                                m.c.send(l);
                                                vibrator.vibrate(new long[]{0, 300}, -1);
                                            } else {

                                                HttpConnection.showToast(getBaseContext(), "命令发送失败，请重试");

                                            }

                                        } else {
                                            HttpConnection.showToast(getBaseContext(), "没有学习状态");


                                        }


                                    } else {

                                        HttpConnection.showToast(getBaseContext(), "服务器错误");

                                    }
                                }
                            });


                } else {
                    h.extra.codeIndex = c.controllerDetail.get(0).num + "";
                    h.extra.settingType = "02";

                    h.extra.powerStatus = flag;
                    h.extra.temperature = code;
                    h.extra.mode = mode;

                    h.extra.speed = speed;

                    l = JSON.toJSONString(h);
                    if (m.flag3) {
                        m.c.send(l);
                        vibrator.vibrate(new long[]{0, 300}, -1);
                    } else {

                        HttpConnection.showToast(getBaseContext(),"命令发送失败，请重试");

                    }
                }


                break;

            case R.id.zidongfeng_kongtiao:
                speed = "01";
                l1.setBackgroundResource(R.drawable.fengzidong);
                if (flag1) {

                    KTutil k = new KTutil();
                    k.controllerId = c.id + "";
                    k.switchFlg = flag;
                    k.speed = speed;
                    k.model = mode;
                    k.temperature = code;

                    String j = JSON.toJSONString(k);
                    j = AESUtil.encrypt(j);
                    HttpUtils httpUtils = new HttpUtils();
                    RequestParams params = new RequestParams();
                    params.setHeader("Content-Type", "application/json");
                    try {
                        params.setBodyEntity(new StringEntity(j, "utf-8"));
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    httpUtils.send(HttpRequest.HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/remote/loadArcCode",
                            params, new RequestCallBack<String>() {

                                @Override
                                public void onFailure(HttpException arg0, String arg1) {
                                    HttpConnection.showToast(getBaseContext(),"网络异常");

                                }

                                @Override
                                public void onSuccess(ResponseInfo<String> arg0) {
                                 if(arg0.result != null && !("".equals(arg0.result))){
                                    arg0.result = AESUtil.decrypt(arg0.result);
                                    JSONObject j = JSONObject.parseObject(arg0.result);
                                    String originalCode = j.getString("originalCode");
                                    if (originalCode != null) {

                                        h.extra.code = originalCode;
                                        String l = JSON.toJSONString(h);
                                        if (m.flag3) {
                                            m.c.send(l);


                                            vibrator.vibrate(new long[]{0, 300}, -1);
                                        } else {

                                            HttpConnection.showToast(getBaseContext(),"命令发送失败，请重试");

                                        }

                                    } else {
                                        HttpConnection.showToast(getBaseContext(),"没有学习状态");


                                    }


                                }else{

                                     HttpConnection.showToast(getBaseContext(),"服务器错误");
                                }}

                            });


                } else {
                    h.extra.codeIndex = c.controllerDetail.get(0).num + "";
                    h.extra.settingType = "03";
                    h.extra.powerStatus = flag;
                    h.extra.temperature = code;
                    h.extra.mode = mode;
                    h.extra.speed = "01";

                    l = JSON.toJSONString(h);
                    if (m.flag3) {
                        m.c.send(l);
                        vibrator.vibrate(new long[]{0, 300}, -1);
                    } else {
                        HttpConnection.showToast(getBaseContext(),"命令发送失败，请重试");

                    }

                }

                break;

            case R.id.digufeng_kongtiao:
                l1.setBackgroundResource(R.drawable.disufeng);
                speed = "02";
                if (flag1) {
                    KTutil k = new KTutil();
                    k.controllerId = c.id + "";
                    k.switchFlg = flag;
                    k.speed = speed;
                    k.model = mode;
                    k.temperature = code;

                    String j = JSON.toJSONString(k);
                    j = AESUtil.encrypt(j);
                    HttpUtils httpUtils = new HttpUtils();
                    RequestParams params = new RequestParams();
                    params.setHeader("Content-Type", "application/json");
                    try {
                        params.setBodyEntity(new StringEntity(j, "utf-8"));
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    httpUtils.send(HttpRequest.HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/remote/loadArcCode",
                            params, new RequestCallBack<String>() {

                                @Override
                                public void onFailure(HttpException arg0, String arg1) {
                                    HttpConnection.showToast(getBaseContext(),"网络异常");

                                }

                                @Override
                                public void onSuccess(ResponseInfo<String> arg0) {
                             if(arg0.result != null && !("".equals(arg0.result))){
                                     arg0.result = AESUtil.decrypt(arg0.result);
                                    JSONObject j = JSONObject.parseObject(arg0.result);
                                    String originalCode = j.getString("originalCode");
                                    if (originalCode != null) {

                                        h.extra.code = originalCode;
                                        String l = JSON.toJSONString(h);
                                        if (m.flag3) {
                                            m.c.send(l);


                                            vibrator.vibrate(new long[]{0, 300}, -1);
                                        } else {

                                            HttpConnection.showToast(getBaseContext(),"命令发送失败，请重试");

                                        }

                                    } else {
                                        HttpConnection.showToast(getBaseContext(),"没有学习状态");


                                    }


                                }else{


                         HttpConnection.showToast(getBaseContext(),"服务器错误");

                                }}

                            });


                } else {
                    h.extra.codeIndex = c.controllerDetail.get(0).num + "";

                    h.extra.settingType = "03";
                    h.extra.powerStatus = flag;
                    h.extra.temperature = code;
                    h.extra.mode = mode;
                    h.extra.speed = speed;
                    l = JSON.toJSONString(h);
                    if (m.flag3) {
                        m.c.send(l);
                    } else {

                        HttpConnection.showToast(getBaseContext(),"命令发送失败，请重试");

                    }

                    vibrator.vibrate(new long[]{0, 300}, -1);
                }

                break;

            case R.id.zhongsu_kongtiao:
                speed = "03";
                l1.setBackgroundResource(R.drawable.zhongsufeng);
                if (flag1) {
                    KTutil k = new KTutil();
                    k.controllerId = c.id + "";
                    k.switchFlg = flag;
                    k.speed = speed;
                    k.model = mode;
                    k.temperature = code;

                    String j = JSON.toJSONString(k);
                    j = AESUtil.encrypt(j);
                    HttpUtils httpUtils = new HttpUtils();
                    RequestParams params = new RequestParams();
                    params.setHeader("Content-Type", "application/json");
                    try {
                        params.setBodyEntity(new StringEntity(j, "utf-8"));
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    httpUtils.send(HttpRequest.HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/remote/loadArcCode",
                            params, new RequestCallBack<String>() {

                                @Override
                                public void onFailure(HttpException arg0, String arg1) {
                                    HttpConnection.showToast(getBaseContext(),"网络异常");

                                }

                                @Override
                                public void onSuccess(ResponseInfo<String> arg0) {
                                    if (arg0.result != null && !("".equals(arg0.result))) {
                                        arg0.result = AESUtil.decrypt(arg0.result);
                                        JSONObject j = JSONObject.parseObject(arg0.result);
                                        String originalCode = j.getString("originalCode");
                                        if (originalCode != null) {

                                            h.extra.code = originalCode;
                                            String l = JSON.toJSONString(h);
                                            if (m.flag3) {
                                                m.c.send(l);


                                                vibrator.vibrate(new long[]{0, 300}, -1);
                                            } else {

                                                HttpConnection.showToast(getBaseContext(), "命令发送失败，请重试");

                                            }

                                        } else {
                                            HttpConnection.showToast(getBaseContext(), "没有学习状态");


                                        }


                                    } else {

                                        HttpConnection.showToast(getBaseContext(), "服务器错误");

                                    }
                                }
                            });


                } else {
                    h.extra.codeIndex = c.controllerDetail.get(0).num + "";
                    h.extra.settingType = "03";
                    h.extra.powerStatus = flag;
                    h.extra.temperature = code;
                    h.extra.mode = mode;
                    h.extra.speed = speed;

                    l = JSON.toJSONString(h);
                    if (m.flag3) {
                        m.c.send(l);

                        vibrator.vibrate(new long[]{0, 300}, -1);
                    } else {

                        HttpConnection.showToast(getBaseContext(),"命令发送失败，请重试");

                    }
                }

                break;

            case R.id.gaofeng_kongtiao:
                speed = "04";
                l1.setBackgroundResource(R.drawable.gaosufeng);
                if (flag1) {
                    KTutil k = new KTutil();
                    k.controllerId = c.id + "";
                    k.switchFlg = flag;
                    k.speed = speed;
                    k.model = mode;
                    k.temperature = code;

                    String j = JSON.toJSONString(k);
                    j = AESUtil.encrypt(j);
                    HttpUtils httpUtils = new HttpUtils();
                    RequestParams params = new RequestParams();
                    params.setHeader("Content-Type", "application/json");
                    try {
                        params.setBodyEntity(new StringEntity(j, "utf-8"));
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    httpUtils.send(HttpRequest.HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/remote/loadArcCode",
                            params, new RequestCallBack<String>() {

                                @Override
                                public void onFailure(HttpException arg0, String arg1) {
                                    HttpConnection.showToast(getBaseContext(),"网络异常");

                                }

                                @Override
                                public void onSuccess(ResponseInfo<String> arg0) {
                                     if(arg0.result != null && !("".equals(arg0.result))){
                                    arg0.result = AESUtil.decrypt(arg0.result);
                                    JSONObject j = JSONObject.parseObject(arg0.result);
                                    String originalCode = j.getString("originalCode");
                                    if (originalCode != null) {

                                        h.extra.code = originalCode;
                                        String l = JSON.toJSONString(h);
                                        if (m.flag3) {
                                            m.c.send(l);


                                            vibrator.vibrate(new long[]{0, 300}, -1);
                                        } else {

                                            HttpConnection.showToast(getBaseContext(),"命令发送失败，请重试");

                                        }

                                    } else {
                                        HttpConnection.showToast(getBaseContext(),"没有学习状态");


                                    }}else{

                                         HttpConnection.showToast(getBaseContext(),"服务器错误");

                                     }


                                }

                            });


                } else {
                    h.extra.codeIndex = c.controllerDetail.get(0).num + "";
                    h.extra.settingType = "03";
                    h.extra.powerStatus = flag;
                    h.extra.temperature = code;
                    h.extra.mode = mode;
                    h.extra.speed = speed;

                    l = JSON.toJSONString(h);
                    if (m.flag3) {
                        m.c.send(l);

                        vibrator.vibrate(new long[]{0, 300}, -1);
                    }
                }

                break;

            case R.id.addwd_kongtiao:

                Integer it = new Integer(code);
                it++;
                if(it>30){
                    it=30;
                }

                code = it + "";
                tv15.setText(code);
                if (flag1) {

                    KTutil k = new KTutil();
                    k.controllerId = c.id + "";
                    k.switchFlg = flag;
                    k.speed = speed;
                    k.model = mode;
                    k.temperature = code;

                    String j = JSON.toJSONString(k);
                    j = AESUtil.encrypt(j);
                    HttpUtils httpUtils = new HttpUtils();
                    RequestParams params = new RequestParams();
                    params.setHeader("Content-Type", "application/json");
                    try {
                        params.setBodyEntity(new StringEntity(j, "utf-8"));
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    httpUtils.send(HttpRequest.HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/remote/loadArcCode",
                            params, new RequestCallBack<String>() {

                                @Override
                                public void onFailure(HttpException arg0, String arg1) {
                                    HttpConnection.showToast(getBaseContext(),"网络异常");

                                }

                                @Override
                                public void onSuccess(ResponseInfo<String> arg0) {
                                    if(arg0.result != null && !("".equals(arg0.result))){
                                    arg0.result = AESUtil.decrypt(arg0.result);

                                    JSONObject j = JSONObject.parseObject(arg0.result);
                                    String originalCode = j.getString("originalCode");
                                    if (originalCode != null) {

                                        h.extra.code = originalCode;
                                        String l = JSON.toJSONString(h);
                                        if (m.flag3) {
                                            m.c.send(l);


                                            vibrator.vibrate(new long[]{0, 300}, -1);
                                        } else {

                                            HttpConnection.showToast(getBaseContext(),"命令发送失败，请重试");

                                        }

                                    } else {
                                        HttpConnection.showToast(getBaseContext(),"没有学习状态");


                                    }


                                }else{

                                        HttpConnection.showToast(getBaseContext(),"网络错误");


                                }}

                            });

                } else {
                    h.extra.codeIndex = c.controllerDetail.get(0).num + "";
                    h.extra.settingType = "06";
                    h.extra.powerStatus = flag;
                    h.extra.temperature = code;
                    h.extra.mode = mode;
                    h.extra.speed = speed;

                    l = JSON.toJSONString(h);
                    if (m.flag3) {
                        m.c.send(l);


                        vibrator.vibrate(new long[]{0, 300}, -1);
                    } else {

                        HttpConnection.showToast(getBaseContext(),"命令发送失败，请重试");

                    }
                }

                break;

            case R.id.jswd_kongtiao:

                Integer it1 = new Integer(code);
                it1--;
                if(it1<16){
                    it1=16;
                }
                code = it1 + "";
                tv15.setText(code);
                if (flag1) {
                    KTutil k = new KTutil();
                    k.controllerId = c.id + "";
                    k.switchFlg = flag;
                    k.speed = speed;
                    k.model = mode;
                    k.temperature = code;
                    String j = JSON.toJSONString(k);
                    j = AESUtil.encrypt(j);
                    HttpUtils httpUtils = new HttpUtils();
                    RequestParams params = new RequestParams();
                    params.setHeader("Content-Type", "application/json");
                    try {
                        params.setBodyEntity(new StringEntity(j, "utf-8"));
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    httpUtils.send(HttpRequest.HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/remote/loadArcCode",
                            params, new RequestCallBack<String>() {

                                @Override
                                public void onFailure(HttpException arg0, String arg1) {
                                    HttpConnection.showToast(getBaseContext(),"网络异常");

                                }

                                @Override
                                public void onSuccess(ResponseInfo<String> arg0) {

                                    if(arg0.result != null && !("".equals(arg0.result))){
                                    arg0.result = AESUtil.decrypt(arg0.result);
                                    JSONObject j = JSONObject.parseObject(arg0.result);
                                    String originalCode = j.getString("originalCode");
                                    if (originalCode != null) {

                                        h.extra.code = originalCode;
                                        String l = JSON.toJSONString(h);
                                        if (m.flag3) {
                                            m.c.send(l);


                                            vibrator.vibrate(new long[]{0, 300}, -1);
                                        } else {

                                            HttpConnection.showToast(getBaseContext(),"命令发送失败，请重试");

                                        }

                                    } else {
                                        HttpConnection.showToast(getBaseContext(),"没有学习状态");


                                    }}else{


                                        HttpConnection.showToast(getBaseContext(),"服务器错误");

                                    }


                                }

                            });

                } else {
                    h.extra.codeIndex = c.controllerDetail.get(0).num + "";
                    h.extra.settingType = "06";
                    h.extra.powerStatus = flag;

                    h.extra.temperature = code;

                    h.extra.mode = mode;
                    h.extra.speed = speed;
                    l = JSON.toJSONString(h);
                    if (m.flag3) {
                        m.c.send(l);
                        vibrator.vibrate(new long[]{0, 300}, -1);
                    } else {

                        HttpConnection.showToast(getBaseContext(),"命令发送失败，请重试");

                    }
                }

                break;

        }
    }

}
